<template>
	<div class="container">
		<h1 class="header">购物车</h1>
		<div class="cart-list">
			<div class="cart-item" v-for="(item, index) in cartList" :key="index">
				<img class="item-img" :src="item.goodsImg" alt="商品图片">
				<div class="item-info">
					<div class="item-name">{{ item.goodsName }}</div>
					<div class="item-price">￥{{ item.price }}</div>
					<div class="item-quantity">
						<button @click="decreaseQuantity(index, item.id, item.num -1 )"><span>-</span></button>
						<input type="number" v-model="item.num" disabled>
						<button @click="increaseQuantity(index, item.id,item.num +1)"><span>+</span></button>
					</div>
				</div>
				<div class="item-subtotal">小计：￥{{ item.price * item.num }}</div>
				<button class="item-delete" @click="deleteItem(item.id)">删除</button>

			</div>
		</div>

		<div class="total-price">总价：￥{{ totalPrice }}</div>
		<button class="checkout-btn" @click="settleAccount()">
			<text>结算</text>
		</button>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				cartList: [],
				pageNum: 1,
				pageSize: 30,
			}
		},
		onLoad() {
			this.cart()
		},
		computed: {
			totalPrice() {
				let total = 0;
				for (let item of this.cartList) {
					total += item.price * item.num;
				}
				return total.toFixed(2);
			}
		},
		methods: {

			settleAccount() {
				let data = {
					name: this.cartList[0].goodsName,
					totalPrice: this.totalPrice,
					carts: this.cartList
				}
				console.log(data)
				this.request.post("/orders", data).then(res => {
					if (res.code === "200") {
						this.$message.success("结算成功,前往订单页面进行支付");
						this.cart();
					} else {
						this.$message.error(res.msg);
					}
				})
			},
			cart() {
				this.request.get("/cart/page", {
					params: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						name: ''
					},
				}).then((res) => {
					if (res.code === "200") {
						console.log("购物车", res)
						this.cartList = res.data.records
					}
				})
			},

			increaseQuantity(index, id, num) {
				console.log(index, id, num)

				this.request.post("/cart/num/" + id + "/" + num).then(res => {
					if (res.code === '200') {
						this.cartList[index].num++;
					}
				});
			},
			decreaseQuantity(index, id, num) {
				console.log(index, id, num)

				this.request.post("/cart/num/" + id + "/" + num).then(res => {
					if (this.cartList[index].num > 1) {
						this.cartList[index].num--;
					}
				});
			},
			deleteItem(id) {
				// this.cartList.splice(index, 1);
				this.request.delete("/cart/" + id).then((res) => {
					if (res.code === "200") {
						this.$message.success("删除成功");
						this.cart();
					} else {
						this.$message.error("删除失败");
					}
				});
			}
		}
	}
</script>

<style scoped>
	.container {
		padding: 20px;
	}

	.header {
		font-size: 32px;
		text-align: center;
		margin-bottom: 20px;
	}

	.cart-list {
		margin-bottom: 20px;
	}

	.cart-item {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.item-img {
		width: 100px;
		height: 100px;
		margin-right: 20px;
	}

	.item-info {
		flex: 1;
	}

	.item-name {
		font-size: 20px;
		margin-bottom: 10px;
	}

	.item-price {
		font-size: 16px;
		color: #999;
		margin-bottom: 10px;
	}

	.item-quantity {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
	}

	.item-quantity button {
		width: 30px;
		line-height: 30px;
		height: 30px;
		background-color: #ccc;
	}

	.item-quantity button span {
		font-size: 16px;
		color: #fff;
	}

	.item-quantity input {
		width: 60px;
		height: 30px;
		text-align: center;
		margin: 0 10px;
		border: none;
		border-radius: 4px;
	}

	.item-subtotal {
		padding-right: 10px;
		font-size: 16px;
		color: #999;
		margin-bottom: 10px;
	}

	.item-delete {
		width: 60px;
		height: 30px;
		font-size: 16px;
		background-color: #f00;
		color: #fff;
	}

	.total-price {
		font-size: 20px;
		text-align: right;
		margin-bottom: 20px;
	}

	.checkout-btn {
		width: 19%;
		margin-right: 1px;
		background-color: orangered;
		color: #fff;
	}

	.checkout-btn text {
		font-size: 16px;
		text-align: center;
	}

	/* 手机端 */
	@media (max-width: 768px) {
		.container {
			padding: 10px;
		}

		.header {
			font-size: 24px;
			margin-bottom: 10px;
		}

		.item-img {
			width: 80px;
			height: 80px;
			margin-right: 10px;
		}

		.item-name {
			font-size: 16px;
			margin-bottom: 5px;
		}

		.item-price {
			font-size: 14px;
			margin-bottom: 5px;
		}

		.item-quantity button {
			line-height: 20px;
			width: 20px;
			height: 20px;
			font-size: 12px;
		}

		.item-quantity input {
			width: 40px;
			height: 20px;
			margin: 0 5px;
		}

		.item-subtotal {
			padding-right: 10px;
			font-size: 14px;
			margin-bottom: 5px;
		}

		.item-delete {
			font-size: 12px;
		}

		.total-price {
			font-size: 16px;
			margin-bottom: 10px;
		}

		.checkout-btn {
			height: 40px;
			font-size: 20px;
		}
	}
</style>
